﻿using UnityEngine;
using System.Collections.Generic;
using UnityEngine.UIElements;

public class GameUI : MonoBehaviour
{
    public int m_MaxIterationTimes = 1000;
    public int m_AIStepIterationTimes = 1;
    public float m_CurrentWinRate;
    public bool m_AutoPlace;
    public Player m_AIPlayer;
    public CellUI[] m_CellUIs;
    CellUI this[int x, int y] => m_CellUIs[x * 11 + y];
    CellUI this[Vector2Int pos] => m_CellUIs[pos.x * 11 + pos.y];

    Game m_Game;
    MonteCarloTreeSearch m_AI;

    private void Start()
    {
        m_Game = new Game();
        for (int i = 0; i < 11; i++)
        {
            for (int j = 0; j < 11; j++)
            {
                this[i, j].m_TargetCell = m_Game.m_Board[i, j];
                this[i, j].m_GameUI = this;
                this[i, j].DisplayCell();
            }
        }
        m_AI = new MonteCarloTreeSearch(m_AIStepIterationTimes);
        Restart();
    }

    private void Update()
    {
        if (!m_Game.m_GameOver)
        {
            for (int i = 0; i < 11; i++)
            {
                for (int j = 0; j < 11; j++)
                {
                    this[i, j].DisplayText("", Player.None);
                }
            }

            if (m_Game.m_CurrentPlayer == m_AIPlayer || m_AIPlayer == Player.All)
            {
                if (m_AI.m_iterationTimes < m_MaxIterationTimes)
                {
                    m_AI.TreeSearchIteration();
                    if (m_AutoPlace && m_AI.m_iterationTimes >= m_MaxIterationTimes)
                    {
                        Vector2Int pos = m_AI.MakeDecision();
                        Place(pos);
                    }
                }

                m_CurrentWinRate = -1f;
                foreach (var child in m_AI.m_root.m_Childrens)
                {
                    if (child.WinRate > m_CurrentWinRate)
                    {
                        m_CurrentWinRate = child.WinRate;
                    }
                }

                foreach (MonteCarloTreeSearch.MonteCarloNode child in m_AI.m_root.m_Childrens)
                {
                    this[child.m_PlacePos].DisplayText($"{child.WinRate.ToString("f2")}({child.m_VisitCount})", m_Game.m_CurrentPlayer);
                }
            }
        }
    }

    public void Restart()
    {
        m_Game.Start();
        if (m_Game.m_CurrentPlayer == m_AIPlayer || m_AIPlayer == Player.All)
            m_AI.StartNew(m_Game, m_Game.m_CurrentPlayer);
    }

    public void Place(Vector2Int pos)
    {
        if (m_Game.m_GameOver) return;
        if (m_Game.m_Board[pos].m_Empty)
        {
            Debug.Log($"player {m_Game.m_CurrentPlayer} place at {pos}");
            m_Game.Step(pos);
            
            this[pos].DisplayCell();
            if (m_Game.m_CurrentPlayer == m_AIPlayer || m_AIPlayer == Player.All)
                m_AI.StartNew(m_Game, m_Game.m_CurrentPlayer);
        }
    }

    // 放好头尾的棋子，中间自动布局
    [ContextMenu("Auto Layout")]
    void AutoLayout()
    {
        Vector3 firstPos = m_CellUIs[0].GetComponent<RectTransform>().localPosition;
        Vector3 lastPos = m_CellUIs[m_CellUIs.Length - 1].GetComponent<RectTransform>().localPosition;
        float interval_x = (lastPos.x - firstPos.x) / 30f;
        float interval_y = (lastPos.y - firstPos.y) / 10f;
        for (int i = 0; i < 11; i++)
        {
            float x = firstPos.x + i * interval_x;
            for (int j = 0; j < 11; j++)
            {
                m_CellUIs[i * 11 + j].GetComponent<RectTransform>().localPosition =
                    new Vector3(x + 2 * j * interval_x, firstPos.y + i * interval_y);
            }
        }
    }
}